﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedHouse.Data.Entities;
using RedHouse.Data.Common;
using RedHouse.Data.Service;
using RedHouse.Data.Base;

namespace RedHouse.AdminControl.UserControls
{
    public partial class UCCategoryDetail : System.Web.UI.UserControl
    {
        private ILanguageService _languageService;
        private ICategoryService _categoryService;
        private ILocationService _locationService;
        public string Message { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            //Service Initialize
            _languageService = new LanguageService();
            _categoryService = new CategoryService();
            _locationService = new LocationService();

            if (!IsPostBack)
            {
                Initialize();
            }

            btnSave.Click += new EventHandler(btnSave_Click);
        }

        private void Initialize()
        {
            Message = string.Empty;
            ErrorPanel.Visible = false;
            SuccessPanel.Visible = false;

            //Databinding
            cboGroup.DataSource = RedHouseGroup.GroupList;
            cboGroup.DataTextField = "Name";
            cboGroup.DataValueField = "Value";
            cboGroup.DataBind();

            cboLocation.DataSource = _locationService.GetAllLocation();
            cboLocation.DataTextField = "Name";
            cboLocation.DataValueField = "ID";
            cboLocation.DataBind();

            cboLanguage.DataSource = _languageService.GetAllLanguage();
            cboLanguage.DataTextField = "Description";
            cboLanguage.DataValueField = "ID";
            cboLanguage.DataBind();


        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            RedHouseCategory category = GetCategoryFromView();
            OperationResult result =  _categoryService.Save(category, true);
            Message = result.Message;
            if (result.Result)
            {
                SuccessPanel.Visible = true;
                ClearForm();
            }
            else
            {
                ErrorPanel.Visible = true;
            }
        }

        private void ClearForm()
        {
            txtTitle.Text = string.Empty;
            txtDescription.Text = string.Empty;
            cboGroup.SelectedIndex = 0;
            cboLanguage.SelectedIndex = 0;
            cboLocation.SelectedIndex = 0;
            txtImageUrl.Text = string.Empty;
        }

        private RedHouseCategory GetCategoryFromView()
        {
            RedHouseCategory category = new RedHouseCategory()
            {
                Group = int.Parse(cboGroup.SelectedValue),
                LangId = new Guid(cboLanguage.SelectedValue),
                Title = txtTitle.Text,
                Description = txtDescription.Text,
                CreatedDate = DateTime.Now,
                CreatedBy = "admin",
                ImageUrl = txtImageUrl.Text,
                LocationId = int.Parse(cboLocation.SelectedValue)
            };
            return category;
        }
    }
}